﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RibbonEditor.Rules;
using System.Xml;

namespace RibbonEditor.Controls.Rules
{
    public partial class CrmClientTypeRuleControl : UserControl, IControlRulable
    {
        private CrmClientTypeRule rule;

        internal CrmClientTypeRule Rule
        {
            get { return this.rule; }
            set { this.rule = value; }
        }

        public CrmClientTypeRuleControl()
        {
            InitializeComponent();
            this.ecClientType.Values = Enum.GetValues(typeof(Enumerations.ClientType));
            this.rule = new CrmClientTypeRule();
        }

        public CrmClientTypeRuleControl(CrmClientTypeRule rule)
            : this()
        {
            this.rule = rule;
            this.FillControls();
        }

        private void FillControls()
        {
            try
            {
                this.bcDefault.Value = this.rule.Default;
                this.bcInvertResult.Value = this.rule.InvertResult;
                this.ecClientType.SelectedValue = this.rule.Type.ToString();
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void Save()
        {
            try
            {
                if (UserControlHelper.IsControlValid(this))
                {
                    this.rule.Default = this.bcDefault.Value;
                    this.rule.InvertResult = this.bcInvertResult.Value;
                    this.rule.Type = (Enumerations.ClientType)Enum.Parse(typeof(Enumerations.ClientType), this.ecClientType.SelectedValue);

                    UserControlHelper.CleanDirtyFields(this);
                }
                else
                {
                    MessageBox.Show(this, "Please fill all required fields", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void BuildObjectXml(XmlNode parentNode)
        {
            try
            {
                this.rule.AddNode(parentNode);
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void CrmClientTypeRuleControl_Leave(object sender, EventArgs e)
        {
            try
            {
                bool isDirty = this.bcDefault.IsDirty || this.bcInvertResult.IsDirty || this.ecClientType.IsDirty;

                if (isDirty)
                {
                    if (DialogResult.Yes == MessageBox.Show(this, "You didn't save your changes! Do you want to save them now?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                    {
                        this.Save();
                        this.bcDefault.IsDirty = false;
                        this.bcInvertResult.IsDirty = false;
                        this.ecClientType.IsDirty = false;
                    }
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
